﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class turtle : log
    {

        bool isDipping;
        //static int x = 1;
        bool canDip;

        protected Animation swimAnim;
        protected Animation dipAnim;
        protected Animation riseAnim;
        //Animation sinkSwimAnim;
        protected PlayerAnimation[] sprite;

        //int size;

        public Boolean IsDipping
        {
            get { return isDipping; }
        }

        float timeDipWait;
        float maxTime = 3500;

        public turtle(Direction dir, Level lvl, float time, float x, float y, float velx, bool Dip, int pace)
            : base(dir, lvl, time, x, y, velx, pace)
        {
            if (dir == Direction.Left)
                pos = new Vector2(1280, y); // comment!!
            else
                pos = new Vector2(x - 240, y);

            setSize();

            //sprite = new PlayerAnimation();
            isDipping = false;
            canDip = Dip;
            this.size = size;
        }

        public virtual void setSize()
        {
            size = 3;

            sprite = new PlayerAnimation[size];

            for (int i = 0; i < size; i++)
                sprite[i] = new PlayerAnimation();
        }

        public override Rectangle colRec
        {
            get
            {
                //return base.colRec;
                return new Rectangle((int)pos.X, (int)pos.Y, 80 * size, 48);
            }
        }

        public override void loadContent(Level lvl)
        {
            //base.loadContent(lvl);

            //tex = lvl.Content.Load<Texture2D>("images/turtle");
            swimAnim = new Animation(lvl.Content.Load<Texture2D>("images/turtleanims"), 0.15f, true, 80,0);
            //sinkSwimAnim = new Animation(lvl.Content.Load<Texture2D>("images/sinkswim"), 0.4f, true, 80);
            dipAnim = new Animation(lvl.Content.Load<Texture2D>("images/turtledipanim"), 0.15f, false, 80, 0);
            riseAnim = new Animation(lvl.Content.Load<Texture2D>("images/turtleriseanim"), 0.15f, false, 80, 0);

            foreach (PlayerAnimation spr in sprite)
            {
                spr.PlayAnimation(swimAnim);
            }
        }

        public override void setSpeed()
        {
            timeWait = (80 * size) / Vel.X * 25 * pace;
        }

        public override void Update(GameTime gameTime, Rectangle viewPort)
        {
            //base.Update(gameTime, viewPort);
            // timetaken is used to store the elapsed time taken from the gameTime object
            // and is compared to the timewaiting variable

            if (!isWaiting) // if the truck isn't waiting
            {
                // then the position on the x axis is increased (or decreased) depending on the enum stored
                switch (dir)
                {
                    case Direction.Left:
                        pos -= vel;
                        break;
                    case Direction.Right:
                        pos += vel;
                        break;
                    default:
                        break;
                }

                
                if (!viewPort.Intersects(colRec)) // if the truck is no longer on screen
                {
                    // it is set to wait
                    isWaiting = true;
                    // its position on the x axis is reset to its starting location
                    switch (dir)
                    {
                        case Direction.Left:
                            pos.X = viewPort.Right;
                            break;
                        case Direction.Right:
                            pos.X = viewPort.Left - (80 * size);
                            break;
                        default:
                            break;
                    }
                }

                if (canDip)
                {
                    if (!isDipping)
                    {
                        timeDipWait += gameTime.ElapsedGameTime.Milliseconds;

                        if (timeDipWait >= (maxTime / 4 * 3))
                        {
                            foreach (PlayerAnimation spr in sprite)
                            {
                                spr.PlayAnimation(dipAnim);
                            }
                        }

                        if (timeDipWait >= maxTime)
                        {
                            isDipping = true;
                            timeDipWait -= maxTime;
                            //sprite.PlayAnimation(swimAnim);
                        }
                    }
                    else
                    {
                        timeDipWait += gameTime.ElapsedGameTime.Milliseconds;

                        if (timeDipWait >= (maxTime / 4 * 3))
                        {
                            foreach (PlayerAnimation spr in sprite)
                            {
                                spr.PlayAnimation(riseAnim);
                            }
                        }

                        if (timeDipWait >= maxTime)
                        {
                            isDipping = false;
                            timeDipWait -= maxTime;
                            foreach (PlayerAnimation spr in sprite)
                            {
                                spr.PlayAnimation(swimAnim);
                            }
                        }
                    }
                }
            }
            else // it's waiting for the time given to wait is over
            {
                // the variable is assigned the elapsed game time from the the gameTime object
                // that is passed as a parameter..
                timeTaken += gameTime.ElapsedGameTime.Milliseconds;

                // ...and is checked to see if is equal or greater than the max amount of time to wait
                if (timeTaken >= timeWait)
                {
                    isWaiting = false; // if it is equal or greater, then its set to not wait (moving)
                    //timeTaken = 0;
                }
            }

        }

        public override checkLocation HandleCollisions(Level lvl, Player player)
        {

            Rectangle bounds = player.plyrRect;

            int playerX = (int)Math.Floor((float)player.Position.X / tile.width);
            int playerY = (int)Math.Ceiling((float)player.Position.Y / tile.height);

            // by using the playerx and playery variables, the enum variable check is assigned
            // what vis returned from the GetCollision method
            collisionCheck check = lvl.GetCollision(playerX, playerY);

            // if the player overlaps the log (calling it a truck for now)
            if (player.plyrRect.Intersects(colRec))
            {
                if (isDipping)
                    return checkLocation.turtledrown; // if the turtles are underwater, the player drowns
                else
                    return checkLocation.onLog; //otherwise the method returns the enum checkLocation.onLog
            }
            else if (check != collisionCheck.passable) //else if the player is on a kill tile
            {
                
                return checkLocation.drowning; // then the drowning enum is returned (kills the player)
                
            }
            else // else the player is on ground
            {
                return checkLocation.onGround;
            }
        }


        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Vector2 posSecond = pos;

            for (int i = 0; i < size; i++)
            {
                if (dir == Direction.Left)
                    sprite[i].Draw(gameTime, spriteBatch, posSecond, SpriteEffects.None, Color.White);
                else
                    sprite[i].Draw(gameTime, spriteBatch, posSecond, SpriteEffects.FlipHorizontally, Color.White);


                posSecond.X += 80;

            }

        }

    }

    class SmallTurtle : turtle
    {
        public SmallTurtle(Direction dir, Level lvl, float time, float x, float y, float velx, bool Dip, int pace)
            : base(dir, lvl, time, x, y, velx, Dip, pace)
        {
        }

        public override void setSpeed()
        {

            timeWait = (80 * size) / Vel.X * 30 * pace;
        }

        public override void setSize()
        {
            size = 2;

            sprite = new PlayerAnimation[size];

            for (int i = 0; i < size; i++)
                sprite[i] = new PlayerAnimation();
        }
    }

}
